package com.jasonzhou.onetomanycontrol.util

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.jasonzhou.onetomanycontrol.base.AppContent
import com.jasonzhou.onetomanycontrol.constant.MessageConstant
import com.tbruyelle.rxpermissions3.RxPermissions

object PermissionsUtils {
    @RequiresApi(Build.VERSION_CODES.S)
    fun android12Bluetooth(activity: FragmentActivity, successCallBack: () -> Unit) {
        RxPermissions(activity).requestEachCombined(
            Manifest.permission.BLUETOOTH_SCAN,
            Manifest.permission.BLUETOOTH_ADVERTISE,
            Manifest.permission.BLUETOOTH_CONNECT
        ).doOnNext {
            when {
                it.granted -> {
                    successCallBack.invoke()
                }

                else -> {
                    AppContent.instance.saveData(MessageConstant.DENIAL_PERMISSION_BLUE, System.currentTimeMillis())
                }
            }
        }.doOnError {
            Log.e("TAG", "doOnError: ${it.message} ")
        }.subscribe()
    }

    @RequiresApi(Build.VERSION_CODES.M)
    fun android6Bluetooth(activity: FragmentActivity, successCallBack: () -> Unit) {
        RxPermissions(activity).requestEachCombined(
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
        ).doOnNext {
            when {
                it.granted -> {
                    successCallBack.invoke()
                }

                else -> {
                    AppContent.instance.saveData(MessageConstant.DENIAL_PERMISSION_BLUE, System.currentTimeMillis())
                }
            }
        }.doOnError {
            Log.e("TAG", "doOnError: ${it.message} ")
        }.subscribe()
    }

    @RequiresApi(api = Build.VERSION_CODES.S)
    fun isGrantedBlueScan(context: Context): Boolean {
        val permissions =
            arrayOf(Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_ADVERTISE)
        val permissionDeniedList: MutableList<String> = ArrayList()
        for (permission in permissions) {
            val permissionCheck = ContextCompat.checkSelfPermission(context, permission)
            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                permissionDeniedList.add(permission)
            }
        }
        return permissionDeniedList.isEmpty()
    }

    fun isGrantedLocation(context: Context): Boolean {
        val permissions =
            arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
        val permissionDeniedList: MutableList<String> = ArrayList()
        for (permission in permissions) {
            val permissionCheck = ContextCompat.checkSelfPermission(context, permission)
            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                permissionDeniedList.add(permission)
            }
        }
        return permissionDeniedList.isEmpty()
    }

    fun isGrantedBluePermission(context: Context): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            isGrantedBlueScan(context)
        } else {
            isGrantedLocation(context)
        }
    }

    fun getLeastMusicPermissions() =
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) arrayOf(Manifest.permission.READ_MEDIA_AUDIO) else arrayOf(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        )

    fun isGrantedStorage(context: Context): Boolean {
        val permissions = getLeastMusicPermissions()
        val permissionDeniedList: MutableList<String> = ArrayList()
        for (permission in permissions) {
            val permissionCheck = ContextCompat.checkSelfPermission(context, permission)
            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                permissionDeniedList.add(permission)
            }
        }
        return permissionDeniedList.isEmpty()
    }
}